package com.sos.logging 
{
	import com.sos.utils.DateUtils;
	
	public class LogTarget
	{
		
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------
		
		protected static const FIELD_SEPARATOR:String		= " ";
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _type:LogTargetType;
		private var _params:LogTargetParams;
		private var _enabled:Boolean;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTOR
		// -------------------------------------------------------------------------------
		
		public function LogTarget(type:LogTargetType, params:LogTargetParams)
		{
			// Init
			_type = type;
			_params = params;
			_enabled = true;
		}
		
		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------
		
		public function get enabled():Boolean
		{
			return _enabled;
		}

		public function set enabled(value:Boolean):void
		{
			_enabled = value;
		}

		public function get type():LogTargetType
		{
			return _type;
		}

		public function get params():LogTargetParams
		{
			return _params;
		}
		
		// -------------------------------------------------------------------------------
		// PROTECTED METHODS
		// -------------------------------------------------------------------------------
		
		protected function isMultiline(message:String):Boolean
		{
			return message.indexOf(Logger.MULTILINE_SEPARATOR) > -1;
		}
		
		protected function split(message:String):Array
		{
			// Splits the message
			var lines:Array = message.split(Logger.MULTILINE_SEPARATOR);
			// Output array
			var output:Array = new Array(lines.length);
			// 
			var headerIndex:int = -1;
			// Counts how many separator are present in the main line formatting
			if (_params.showLevel)		headerIndex = message.indexOf(FIELD_SEPARATOR, headerIndex + 1);
			if (_params.showDate) 	  	headerIndex = message.indexOf(FIELD_SEPARATOR, headerIndex + 1);
			if (_params.showTime)	  	headerIndex = message.indexOf(FIELD_SEPARATOR, headerIndex + 1);
			// Check if the header is present copy it and adds it to all the lines
			if (headerIndex > -1)
			{
				// Get the header
				var header:String = message.substring(0, headerIndex + 1);
				// Copy the first line
				output[0] = lines[0];
				// adds the header to all the log lines
				for (var i:int=1; i < lines.length; i++)
				{
					output[i] = header + lines[i];
				}
			}
			else
			{
				// Use the original array
				output = lines;
			}
			return output;
		}
		
		/**
		 * Abstract method, must be implemented in LogTarget implementation 
		 * @param message 
		 */		
		protected function write(message:String):void
		{
			throw new Error("LogTarget.internalLog() NOT IMPLEMENTED!");
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function logEvent(event:LogEvent):void
		{
			// Check if the LogTarget is enabled
			if (_enabled)
			{
				// Check if the LogTarget handles the current log level
				if (event.level.valid(_params.level))
				{
					// Log datetime formatting
					var date:String = "";
					if (_params.showDate && _params.showTime)
					{
						date = DateUtils.format(new Date(), _params.dateFormat + " " + _params.timeFormat) + FIELD_SEPARATOR;
					}
					else if (_params.showDate)
					{
						date = DateUtils.format(new Date(), _params.dateFormat) + FIELD_SEPARATOR;
					}
					else if (_params.showTime)
					{
						date = DateUtils.format(new Date(), _params.timeFormat) + FIELD_SEPARATOR;
					}
					// Log level formatting
					var level:String = "";
					if (_params.showLevel)
					{
						level = "[" + event.level.name + "]" + FIELD_SEPARATOR;
					}
					// Call internal abstract method to write log
					write(date + level + event.message);
				}
			}
		}
		
	}
}